<?php

namespace Crm\Model;

/**
 * 自定义扩展字段数据库模型
 *
 * @author SoChishun <14507247@qq.com>
 * @version 1.0
 * @since 1.0.0 <2015-4-9> SoChishun Added.
 */
class CommonExfieldModel extends AppbaseModel {

    /**
     * 数据表名称
     * @var string
     */
    protected $tableName = 'tcommon_exfield';

    /**
     * 字段定义
     * @var array
     */
    protected $fields = array('id', 'field', 'type', 'length', 'null', 'default', 'comment', 'target', 'label', 'input_type', 'optional_value', 'is_require', 'is_visiable', 'allow_search', 'ordinal', 'state', 'create_time', 'update_time', 'site_id', '_pk' => 'id', '_autoinc' => true);

    /**
     * 命名范围
     * @var array
     */
    protected $_scope = array(
        'default' => array(
            'field' => array('field', 'type', 'length', 'default', 'label', 'input_type', 'optional_value', 'is_visiable'),
            'order' => 'ordinal, id',
        ),
    );

    /**
     * 是否批量验证
     * @var boolean
     */
    protected $patchValidate = true;

    /**
     * 验证规则
     * @var array
     */
    protected $_validate = array();

    /**
     * 自动填充
     * @var array
     */
    protected $_auto = array();

    /**
     * 返回字段列表
     * @param int $site_id
     * @param string $target
     * @return false|array
     * @since 1.0 <2015-4-9> SoChishun Added.
     */
    public function get_list($site_id, $target) {
        return $this->scope()->where(array('site_id' => $site_id, 'target' => $target))->select();
    }

    /**
     * 返回表单数据
     * @param int $site_id
     * @param string $target
     * @param null|array $data
     * @return false|array
     * @since 1.0 <2015-4-9> SoChishun Added.
     */
    public function get_form_data($site_id, $target, $data = null) {
        if (is_null($data)) {
            $data = $_POST;
        }
        if (!$data) {
            $this->error[] = '数据源无效';
            return false;
        }
        $fields = $this->get_list($site_id, $target);
        if (!$fields) {
            $this->error[] = '无字段';
            return false;
        }
        $values = false;
        $types = array('int' => 'number', 'smallint' => 'number', 'decimal' => 'double', 'date' => 'date', 'datetime' => 'datetime');
        foreach ($fields as $row) {
            $name = trim($row['field']);
            if (!$name || !isset($data[$name])) {
                continue;
            }
            $type = $row['type'];
            $rule = array_key_exists($type, $types) ? $types[$type] : 'require';
            $value = $data[$name];
            if (is_array($value)) {
                $value = implode(',', $value);
            }
            if ($row['isRequire']) {
                if (!$this->check($value, $rule)) {
                    $this->error[] = $row['label'] . '无效';
                    continue;
                }
            } else {
                if ($value && !$this->M->check($value, $rule)) {
                    $this->error[] = $row['label'] . '错误';
                    continue;
                }
            }
            $values[$name] = $value;
        }
        return $values;
    }

    public function add_item() {
        
    }

}
